package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.IsDeletedFlagEnum;
import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import com.jingdianjichi.subject.domain.convert.RadioSubjectConverter;
import com.jingdianjichi.subject.domain.entity.SubjectAnswerBO;
import com.jingdianjichi.subject.domain.entity.SubjectInfoBO;
import com.jingdianjichi.subject.domain.entity.SubjectOptionBO;
import com.jingdianjichi.subject.infra.basic.entity.SubjectRadio;
import com.jingdianjichi.subject.infra.basic.service.SubjectRadioService;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 单选的题目策略类
 */
@Component
public class RadioTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectRadioService radioService;

    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.RADID;       //告诉你我这个Hadnler制作单选的
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        //单选题目的插入,因为是ABCD多个选项的
        List<SubjectRadio> subjectRadioList=new ArrayList<>();

        subjectInfoBO.getOptionList().forEach(option ->{
            //转换
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.convertBoToEntity(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

            subjectRadioList.add(subjectRadio);
        });

        radioService.batchInsert(subjectRadioList);
    }

    @Override
    public void upDate(SubjectInfoBO subjectInfoBO) {
        //单选题目的插入,因为是ABCD多个选项的
        List<SubjectRadio> subjectRadioList=new ArrayList<>();

        subjectInfoBO.getOptionList().forEach(option ->{
            //转换
            SubjectRadio subjectRadio = RadioSubjectConverter.INSTANCE.convertBoToEntity(option);
            subjectRadio.setSubjectId(subjectInfoBO.getId());
            subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

            subjectRadioList.add(subjectRadio);
        });

        radioService.batchUpdate(subjectRadioList);
    }

    @Override
    public SubjectOptionBO query(long subjectId) {
        SubjectRadio subjectRadio = new SubjectRadio();
        subjectRadio.setSubjectId(subjectId);
        subjectRadio.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());

        List<SubjectRadio> result = radioService.queryByCondition(subjectRadio);
        List<SubjectAnswerBO> subjectAnswerBOList = RadioSubjectConverter.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
    }
}
